<?php


namespace api\web\controller;


use api\web\model\BannerModel;
use api\web\model\EducationModel;
use api\web\model\MessageModel;
use api\web\model\NavModel;
use api\web\model\NoticeModel;
use api\web\model\WorkModel;
use app\wenchuang\model\PolicyModel;
use app\wenchuang\model\TheoryModel;
use cmf\controller\RestBaseController;

class IndexController extends RestBaseController
{
      //获取导航栏菜单
      public function nav()
      {
          $nav = new NavModel();
          $data = $nav->order('list_order','desc')
          ->field('id,name,url')
          ->select();
          $this->success('导航栏',$data);
      }
      //获取轮播图
      public function banner()
      {
        $banner = new BannerModel();
        $data = $banner
            ->field('id,image,url')
            ->select()->toArray();

        //转化数据库保存图片的文件路径，为可以访问的url
        foreach ($data as $k => $v)
        {
            $data[$k]['image'] = cmf_get_image_url($data[$k]['image']);
        }
        $this->success('轮播图', $data);
      }
      //获取工作动态
      public function work()
      {
          $pageSize = $this->request->param('page_size/d',10);
          //接收页数（默认第一页）
          $page = $this->request->param('page/d',1);
          $workModel = new WorkModel();
          $work = $workModel
          ->order(['list_order' => 'desc' , 'create_time' => 'desc'])
          ->field('id,title,abstract,image,content,author,read_num,create_time')
          ->page($page,$pageSize)
          ->select();
          //转化数据库保存图片的文件路径，为可以访问的url
          foreach ($work as $k => $v)
          {
              $work[$k]['image'] = cmf_get_image_url($work[$k]['image']);
          }
          $this->success('工作动态', $work);
      }
      //获取通知公告
      public function notice()
      {

          $noticeModel = new NoticeModel();
          $notice = $noticeModel
              ->order(['list_order' => 'desc', 'create_time' => 'desc'])
              ->field('id,title,abstract,image,content,author,read_num,create_time')
              ->select();
          //转化数据库保存图片的文件路径，为可以访问的url
          foreach ($notice as $k => $v) {
              $notice[$k]['image'] = cmf_get_image_url($notice[$k]['image']);
          }
          //富文本字段显示规范
          $noticeData = [];
          foreach ($notice as $k => $v) {
              $noticeData[$k] = $v->toArray(); // 转换对象为数组
              $noticeData[$k]['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($v['content']));
          }
          $this->success('通知公告', $noticeData);
      }


      //获取廉政教育文章
      public function education()
      {

          $EducationModel = new EducationModel();
          $education = $EducationModel
              ->order(['list_order' => 'desc', 'create_time' => 'desc'])
              ->field('id,title,abstract,image,content,author,read_num,create_time')
              ->select();
          //转化数据库保存图片的文件路径，为可以访问的url
          foreach ($education as $k => $v) {
              $education[$k]['image'] = cmf_get_image_url($education[$k]['image']);
          }
          //富文本字段显示规范
          $educationData = [];
          foreach ($education as $k => $v) {
              $educationData[$k] = $v->toArray(); // 转换对象为数组
              $educationData[$k]['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($v['content']));
          }
          $this->success('廉政教育', $educationData);

      }

    //获取理论学习文章
    public function theory()
    {

        $TheoryModel = new TheoryModel();
        $Theory = $TheoryModel
            ->order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,content,author,read_num,create_time')
            ->select();
        //转化数据库保存图片的文件路径，为可以访问的url
        foreach ($Theory as $k => $v) {
            $Theory[$k]['image'] = cmf_get_image_url($Theory[$k]['image']);
        }
        //富文本字段显示规范
        $TheoryData = [];
        foreach ($Theory as $k => $v) {
            $TheoryData[$k] = $v->toArray(); // 转换对象为数组
            $TheoryData[$k]['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($v['content']));
        }
        $this->success('廉政教育', $TheoryData);

    }

    //获取政策法规文章
    public function policy()
    {

        $PolicyModel = new PolicyModel();
        $Policy = $PolicyModel
            ->order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,content,author,read_num,create_time')
            ->select();
        //转化数据库保存图片的文件路径，为可以访问的url
        foreach ($Policy as $k => $v) {
            $Policy[$k]['image'] = cmf_get_image_url($Policy[$k]['image']);
        }
        //富文本字段显示规范
        $PolicyData = [];
        foreach ($Policy as $k => $v) {
            $PolicyData[$k] = $v->toArray(); // 转换对象为数组
            $PolicyData[$k]['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($v['content']));
        }
        $this->success('廉政教育', $PolicyData);
    }
    public function message(){
        $this->success(MessageModel::select());
    }
}